package com.ruoyi.web.controller.suggestion;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.points.domain.CommunityPointsLog;
import com.ruoyi.points.domain.CommunityUserPoints;
import com.ruoyi.points.service.ICommunityPointsLogService;
import com.ruoyi.points.service.ICommunityUserPointsService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.suggestion.domain.CommunitySuggestion;
import com.ruoyi.suggestion.service.ICommunitySuggestionService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
//import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysUserService;

/**
 * 社区建议Controller
 *
 * @author xianyu
 * @date 2025-04-02
 */
@RestController
@RequestMapping("/suggestion/suggestion")
public class CommunitySuggestionController extends BaseController
{
    @Autowired
    private ICommunitySuggestionService communitySuggestionService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询社区建议列表
     */
    @PreAuthorize("@ss.hasPermi('suggestion:suggestion:list')")
    @GetMapping("/list")
    public TableDataInfo list(CommunitySuggestion communitySuggestion) {
        startPage();
        List<CommunitySuggestion> list = communitySuggestionService.selectCommunitySuggestionList(communitySuggestion);
//        for (CommunitySuggestion suggestion : list) {
////            System.out.println(suggestion);
//            break;
//        }
        return getDataTable(list); // 返回带用户名的列表
    }
    /**
     * 报表
     */
    @PreAuthorize("@ss.hasPermi('suggestion:suggestion:list')")
    @GetMapping("/dashboard")
    public ResponseEntity<List<TableDataInfo>> dashboard() {
        List<TableDataInfo> dashboardStats = communitySuggestionService.getDashboardStats();

        return new ResponseEntity<>(dashboardStats, HttpStatus.OK);
    }
    /**
     * 导出社区建议列表
     */
    @PreAuthorize("@ss.hasPermi('suggestion:suggestion:export')")
    @Log(title = "社区建议", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommunitySuggestion communitySuggestion)
    {
        List<CommunitySuggestion> list = communitySuggestionService.selectCommunitySuggestionList(communitySuggestion);
        ExcelUtil<CommunitySuggestion> util = new ExcelUtil<CommunitySuggestion>(CommunitySuggestion.class);
        util.exportExcel(response, list, "社区建议数据");
    }

    /**
     * 获取社区建议详细信息
     */
    @PreAuthorize("@ss.hasPermi('suggestion:suggestion:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(communitySuggestionService.selectCommunitySuggestionById(id));
    }

    /**
     * 新增社区建议
     */
    @Autowired
    private ICommunityUserPointsService userPointsService;

    @Autowired
    private ICommunityPointsLogService pointsLogService;
    @PreAuthorize("@ss.hasPermi('suggestion:suggestion:add')")
    @Log(title = "社区建议", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CommunitySuggestion communitySuggestion)
    {
        // 设置默认值
        communitySuggestion.setViewCount(0L);
        communitySuggestion.setEarnedPoints(20L);
        // 设置提交人为当前用户ID
        communitySuggestion.setUserId(getUserId());

        // 先插入社区建议
        int insertResult = communitySuggestionService.insertCommunitySuggestion(communitySuggestion);
        if (insertResult <= 0) {
            return error("新增社区建议失败");
        }

        // 增加20积分
        Long userId = getUserId();
        CommunityUserPoints userPoints = userPointsService.selectByUserId(userId);
        if (userPoints == null) {
            userPoints = new CommunityUserPoints();
            userPoints.setUserId(userId);
            userPoints.setPoints(20L);
            userPointsService.insertCommunityUserPoints(userPoints);
        } else {
            userPoints.setPoints(userPoints.getPoints() + 20L);
            userPointsService.updateCommunityUserPoints(userPoints);
        }

        // 记录积分日志
        CommunityPointsLog pointsLog = new CommunityPointsLog();
        pointsLog.setUserId(userId);
        pointsLog.setPoints(20L);
        pointsLog.setSourceType("suggestion");
        pointsLog.setSourceId(communitySuggestion.getId()); // 获取插入建议的ID
        pointsLogService.insertCommunityPointsLog(pointsLog);

        return toAjax(insertResult);
    }

    /**
     * 修改社区建议
     */
    @PreAuthorize("@ss.hasPermi('suggestion:suggestion:edit')")
    @Log(title = "社区建议", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CommunitySuggestion communitySuggestion)
    {
        return toAjax(communitySuggestionService.updateCommunitySuggestion(communitySuggestion));
    }

    /**
     * 删除社区建议
     */
    @PreAuthorize("@ss.hasPermi('suggestion:suggestion:remove')")
    @Log(title = "社区建议", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(communitySuggestionService.deleteCommunitySuggestionByIds(ids));
    }
    //投票信息
    @PreAuthorize("@ss.hasPermi('suggestion:suggestion:list')")
    @GetMapping("/suggestVote")
    public ResponseEntity<List<TableDataInfo>> suggestVote() {
        List<TableDataInfo> suggestVotes = communitySuggestionService.suggestVote();

        return new ResponseEntity<>(suggestVotes, HttpStatus.OK);
    }
}
